﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DomusDotNet.Sbm.Client.SL4.ViewModels;
using Moq;
using SharpTestsEx;
using DomusDotNet.Sbm.Client.SL4.WCFServices;
using DomusDotNet.Sbm.Client.SL4.Infrastructure;
using System.Collections.ObjectModel;
using Caliburn.Micro;

namespace DomusDotNet.Sbm.Client.SL4.Tests
{
    [TestClass]
    public class the_SearchViewModel : TestBase
    {

        [TestMethod]
        public void should_clear_search()
        {

            var sut = new SearchViewModel(null, null, null);
            sut.Criteria.TitleFilter = "some title";
            sut.Books.AddRange(new[] {
                new BookBase(),
                new BookBase(),
            });

            sut.Clear();

            sut.Criteria.TitleFilter.Should().Be.NullOrEmpty();
            sut.Books.Should().Be.Empty();
        }

        [TestMethod]
        public void should_call_service_with_criteria()
        {
			var sut = new SearchViewModel(null, null, null);
            sut.Criteria.TitleFilter = "some title";

            TestExecutorFor(sut.Search())
                .Next<WCFServiceCallResult>(x =>
                {
                    x.Should().Not.Be.Null();
                    x.Request.Should().Be.OfType<SearchBooksRequest>();

                    x.GetRequest<SearchBooksRequest>().TitleFilter
                        .Should().Be.EqualTo("some title");
                });
        }

        [TestMethod]
        public void should_handle_service_errors()
        {
			var sut = new SearchViewModel(null, null, null);

            TestExecutorFor(sut.Search())
                .Next<WCFServiceCallResult>(x =>
                {
                    x.Response = new SearchBooksResponse
                    {
                        Result = false,
                        Errors = new ObservableCollection<Error>{ 
                            new Error{ Message = "error1"} ,
                            new Error{ Message = "error2"} 
                        }
                    };
                })
				.Finish();


			sut.ErrorMessage.Should().Be.EqualTo("error1\nerror2");
        }

        [TestMethod]
        public void should_open_detail_on_single_result()
        {
            var shell = new Mock<IShell>();
			var sut = new SearchViewModel(shell.Object, null, ()=> new BookViewModel(shell.Object));

            var singleBook = new BookBase();


            shell.Setup(x => x.ActivateItem(It.IsAny<BookViewModel>()))
                .Callback((IScreen vm) => {

                    vm.Should().Be.OfType<BookViewModel>();
                    ((BookViewModel)vm).Book.Should().Be.EqualTo(singleBook); 
                
                });


            TestExecutorFor(sut.Search())
                .Next<WCFServiceCallResult>(x =>
                {
                    x.Response = new SearchBooksResponse
                    {
                        Result = true,
                        Books = new ObservableCollection<BookBase> { singleBook }
                    };
                })
                .Finish();

            
        }

        [TestMethod]
        public void should_load_result_list_with_result()
        {
			var sut = new SearchViewModel(null, null, null);

            var books = new[]  {
                new BookBase(),
                new BookBase(),
                new BookBase(),
            };
            TestExecutorFor(sut.Search())
                .Next<WCFServiceCallResult>(x =>
                {
                    x.Response = new SearchBooksResponse
                    {
                        Result = true,
                        Books = new ObservableCollection<BookBase>(books)
                    };
                })
                .Finish();

            sut.Books.Should().Have.SameSequenceAs(books);
        }

    }
}
